$(function () {
    $("body").on("touchmove", function (event) { //禁止页面 下拉
        event.preventDefault();
    })
    var Page = (function () {
        var config = {
                $bookBlock: $('#bb-bookblock'),
                $navNext: $('#bb-nav-next'),
                $navPrev: $('#bb-nav-prev'),
                $navFirst: $('#bb-nav-first'),
                $navLast: $('#bb-nav-last')
            },
            init = function () {
                config.$bookBlock.bookblock({
                    speed: 500,
                    shadowSides: 0.8,
                    shadowFlip: 0.7,
                    interval: 300
                });
                initEvents();
            },
            initEvents = function () {

                var $slides = config.$bookBlock.children();

                // add navigation events
                config.$navNext.on('click touchstart', function () {
                    config.$bookBlock.bookblock('next');
                    return false;
                });

                config.$navPrev.on('click touchstart', function () {
                    config.$bookBlock.bookblock('prev');
                    return false;
                });

                config.$navFirst.on('click touchstart', function () {
                    config.$bookBlock.bookblock('first');
                    return false;
                });

                config.$navLast.on('click touchstart', function () {
                    config.$bookBlock.bookblock('last');
                    return false;
                });

                // add swipe events
                $slides.on({
                    'swipeleft': function (event) {
                        config.$bookBlock.bookblock('next');
                        return false;
                    },
                    'swiperight': function (event) {
                        config.$bookBlock.bookblock('prev');
                        return false;
                    }
                });

                // add keyboard events
                $(document).keydown(function (e) {
                    var keyCode = e.keyCode || e.which,
                        arrow = {
                            left: 37,
                            up: 38,
                            right: 39,
                            down: 40
                        };

                    switch (keyCode) {
                        case arrow.left:
                            config.$bookBlock.bookblock('prev');
                            break;
                        case arrow.right:
                            config.$bookBlock.bookblock('next');
                            break;
                    }
                });
            };

        return {
            init: init
        };

    })();
    Page.init();
    set_ani_go();
    (function () {
        var home = $('div.home'),
            main = $('div.main'),
            hand = $('img.hand');
        window.location.hash = 'home';
        $('div.imgs')
            .find('div.cheats')
            .on('webkitAnimationEnd', function (event) {
                event.stopPropagation();
                if (event.target === this) {
                    hand.css('visibility', 'visible')
                        .addClass('hand_go');
                }
            })
            .end()
            .find('img.book')
            .on('click', function (event) {
                event.stopPropagation();
                home.css('display', 'none')
                main.css('display', 'block')
                    .addClass('main_go');
                window.location.hash = 'book';
                hand.css('visibility', 'hidden')
                    .removeClass('hand_go');
            })
        var go = function () {
            if (window.location.hash === '#home') {
                home.css('display', 'block')
                main.css('display', 'none')
                    .removeClass('main_go');
                Page.init();
                $('#bb-bookblock').bookblock('first');
                // $('#bb-bookblock').bookblock('first');
                // console.log(111111)
                fitContent(0)
            } else if (window.location.hash === '#book') {
                $('img.book').click();
            }
        }
        $(window).on('hashchange', function (event) {
            event.stopPropagation();
            go();
        })
            .on('unload',function (event) {
                event.stopPropagation();
                go();
             })
    })()
})

function set_ani_go() {
    $('div.dan img').addClass('dan_go');
    $('div.home .cheats').addClass('cheats_go');
    $('.home .cheats img:nth-child(1)').addClass('cheats_img_go');
    $('.home .cheats img:nth-child(2)').addClass('book_go');
    $('.home  img.prompt').addClass('prompt_go');
}